//页面加载完后响应onReady事件
$(function(){	
	var dateFmt = "";
	var currentDate = "";
	var today = new Date();
	var year = today.getFullYear();
	var month = today.getMonth()+1;
	var day = today.getDate();
	
	if (month >=0 && month <10)
	{
		month = "0"+month;
	}
	if (day >=0 && day <10)
	{
		day = "0"+day;
	}
	
	//周期类型: 2(月)  3(日)
	//当周期类型为月时,默认日期是当前月
	//当周期类型为日时，默认日期是当天
	var periodType = $("#periodType").val();
	if (periodType == 2)
	{
		dateFmt = "yyyyMM";
		currentDate = year + ""+month;
	}
	else if (periodType == 3)
	{
		dateFmt = "yyyyMMdd";
		currentDate = year+""+month+""+day;
	}
	$("#dataDate").val(currentDate);
	
	//格式化日期函数
    $('#dataDate').my97({
		  dateFmt:dateFmt,
		  startDate:[{
			 doubleCalendar:false,
			 isShowWeek:false,
			 isShowClear:false,
			 readOnly:true
		  }]
	 });
    
    //初始化时加载该报表下所有的运行结果
	getReportResult();
});	


/**
 * 加载该报表下的运行结果
 * @param ruleName 规则名称
 * @param dataDate 日期
 */
function getReportResult(){
	$.ajax({
		url:ctx+'/urgentrptmgr/speedReportAction!getReportResult.action',
		data:{reportId:$('#reportid').val(),dataDate:$("#dataDate").val(),reportType:$("#reportType").val()
			,periodType:$("#periodType").val()},
		dataType:'json',
		beforeSend:function(){
			$.messager.progress({title : '提示',text : '正在加载数据，请稍候 ....'});
		},
		error:function(){
			$.messager.progress('close');
		},
		success:function(data){						
			createGZInfo(data);	
			createDiv(data);
		},
		complete:function(){
			$.messager.progress('close');
		}
	});
}

/**
 * 显示一个规则的所有维度信息
 * @param id
 */
function showDimInfo(id)
{
	//div对象不存在
	if ($("#div_"+id).length == 0)
	{
		//隐藏所有浮动层div
		$("div.div_dim").hide();
		$("div.div_dim2").mCustomScrollbar("destroy");
		$("div.right_arrow").css({"display":"none"});
		return;
	}
	
	//div对象存在并且隐藏
	if ($("#div_"+id).css("display") == "none")
	{
		//隐藏所有浮动层div
		$("div.div_dim").hide();
		$("div.div_dim2").mCustomScrollbar("destroy");
		$("div.right_arrow").css({"display":"none"});
		
		//弹出层left
		var div_left = 15;
		//弹出层top
		var div_top = 0;
		//箭头高度
		var arrow_top = 0;
		//箭头位置 up:上   down:下
		var arrow_position = "";
		
		
		var current_top = $("#table_"+id).offset().top-$("#showGZDiv").offset().top;
		//当前元素的偏移量-父元素的偏移量 +弹出层的高度*3/4>父元素的高度
		if (current_top+$("#div_"+id).height()*3/4 >$("#showGZDiv").height())
		{
			arrow_position = "down";
		}
		else
		{
			arrow_position = "up";
		}
		//80=日期选择所在区域的高度
		if (arrow_position == "up")
		{
			if (current_top > $("#div_"+id).height()/4)
			{
				div_top = current_top + 80 - $("#div_"+id).height()/4;
			}
			else
			{
				div_top = 90;
			}
			arrow_top = $("#div_"+id).height()/4;
		}
		else
		{
			div_top = current_top + 80 - $("#div_"+id).height()*3/4;
			if (div_top <= 80)
			{
				div_top = 90;
			}
			arrow_top = $("#div_"+id).height()*3/4;
		}
		
		
		$("#div_"+id).find("div.right_arrow").css({top:arrow_top});
		$("#div_"+id).find("div.right_arrow").css({"display":"block"});
		$("#div_"+id).css({left:div_left, top:div_top});
		$("#div_"+id).css({"display":"block"});
		$("#div_"+id).addClass("div_dim");
		$("#div_"+id).find("table.table_dim tr:odd").addClass("odd"); 
		$("#div_"+id).find("table.table_dim tr:even").addClass("even"); 
		
		$("#div_"+id).find("table.table_dim tr.real-line").each(function(){
			if ($(this).hasClass("odd"))
			{
				$(this).css("background-color", "white");
			}
			else
			{
				$(this).css("background-color", "#f3f8fc");
			}
		});
		
		$("#div_"+id).find("div.div_dim2").mCustomScrollbar({
			theme: "light-thick",
			scrollInertia:0,
			autoDraggerLength:true,
			scrollButtons:{
				enable:false
			}
		});
		
		//当鼠标移出弹出层的区域之外时关闭弹出层
		$("#div_"+id).mouseleave(function(){
			hideDiv(id);
		});
	}
	
}


function hideDiv(id)
{
	if ($("#div_"+id).css("display") == "block")
	{
		$("#div_"+id).hide();
		$("#div_"+id).find("div.left_arrow").css({"display":"none"});
		$("#div_"+id).find("div.right_arrow").css({"display":"none"});
		$("#div_"+id).find("div.div_dim2").mCustomScrollbar("destroy");
	}
}

//规则信息
function createGZInfo(data)
{
var allLifeInfoHTML="";
	
	allLifeInfoHTML +='<div class="head-warp" style="padding-top:10px;*border-bottom:1px dashed #40A5D7;height:0px">   ';                                          
	allLifeInfoHTML +='  <div class="head">       ';                                                                                                               
	allLifeInfoHTML +='        <div class="nav-box" >     ';                                                                                                       
	allLifeInfoHTML +='          <ul>     ';                                                                                                                       
	allLifeInfoHTML +='              <li class="cur" style="text-align:left;padding-top:7px;padding-left:17px;font-size:30px;" ></li>';
	allLifeInfoHTML +='          </ul>     ';                                                                                                                      
	allLifeInfoHTML +='        </div>     ';                                                                                                                       
	allLifeInfoHTML +='  </div>      ';                                                                                                                            
	allLifeInfoHTML +='</div>      ';      
	allLifeInfoHTML +=' <div class="main" style="padding-left:12px;padding-top:20px">';
	allLifeInfoHTML +='  <div class="history" id="history" style="margin-left: 140px;">';
	allLifeInfoHTML +=' <div class="history-date">';
	allLifeInfoHTML +='  <ul>';
	allLifeInfoHTML +='	<div class="first"><a href="#nogo"></a></div>';
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </br>"; 
	
	if(data != null && data != "")
	{
		for(var i=0;i<data.length;i++)
		{
			allLifeInfoHTML+='<li class="green">';
			allLifeInfoHTML +='<h3><span>'+data[i].checktime+getCheckTimeUnit()+'</span></h3>';
			allLifeInfoHTML +='<dl>';
			allLifeInfoHTML +='<dt>';
			
			allLifeInfoHTML +='<table id="table_'+data[i].ruleid+'" class="showGZ" border="0" onmouseover="showDimInfo(\''+data[i].ruleid+'\')">';
			allLifeInfoHTML +='<tr>';
			allLifeInfoHTML +='<td width="65px" height="44px">';
			allLifeInfoHTML +='</td>';
			allLifeInfoHTML +='<td align="left"><span class="showGZ-text1" style="color: #4f8fc7;font-size: 14px;" title="'+data[i].ruleName+'">'+data[i].ruleName+'</span>';
			allLifeInfoHTML +='</td>';
			if (data[i].reportResultList!=null && data[i].reportResultList!="")
			{
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML+= "<span>("+data[i].reportResultList.length+"个运行结果)</span>";
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='<td align="left" style="padding-left: 5px;">日期：'+data[i].reportResultList[0].dataDate;
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML+='<td align="left">告警级别：<label class="'+warnLevelColor(data[i].reportResultList[0].warnLevel)+'" >'+data[i].reportResultList[0].warnLevelName+'</label></td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML+='<td colspan="2" align="left" style="padding-left: 5px;"><span style="float:left;">维度：</span><span class="showGZ-text2" title="'+data[i].reportResultList[0].dimInfo+'">'+data[i].reportResultList[0].dimInfo+'</span></td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='<td colspan="2" align="left" style="padding-left: 5px;"><span style="float:left;">运行结果：</span><span class="showGZ-text2" title="'+data[i].reportResultList[0].ruleContent+'">'+data[i].reportResultList[0].ruleContent+'</span>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='</tr>';
			}
			else
			{
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML+='<td align="left" style="padding-left: 5px;">日期：</td>';
				allLifeInfoHTML+='<td align="left">告警级别：</td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML+='	<td colspan="2" align="left" style="padding-left: 5px;">维度：</td>';
				allLifeInfoHTML +='</tr>';
				
				allLifeInfoHTML +='<tr>';
				allLifeInfoHTML +='<td>';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='<td colspan="2" align="left" style="padding-left: 5px;">运行结果：';
				allLifeInfoHTML +='</td>';
				allLifeInfoHTML +='</tr>';
			}
			
			
			allLifeInfoHTML +='</table>';
			allLifeInfoHTML +='</dt>';
			allLifeInfoHTML +='</dl>';
			allLifeInfoHTML +='</li>';
	   
		}
	}
	
	allLifeInfoHTML +="	  </ul>"; 
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </br>"; 
	allLifeInfoHTML +="	  </div>"; 
	allLifeInfoHTML +="	  </div>"; 
	allLifeInfoHTML +="	  </div>"; 
	$("#showGZDiv").html(allLifeInfoHTML);
	
//	$("#showGZDiv").mCustomScrollbar({
//		theme: "light-thick",
//		scrollButtons:{
//			enable:false
//		},
//		callbacks:{
//			whileScrolling:function(){
//				//判断是否有弹出层div显示在页面，如果有关闭弹出层
//				$("div.div_dim:visible").each(function(){
//					$(this).hide();
//					$(this).children("div.div_dim2").mCustomScrollbar("destroy");
//					$(this).children("div.right_arrow").css({"display":"none"});
//				});
//			}
//		}
//	});
	
	$("#showGZDiv").scroll(function(){
		//判断是否有弹出层div显示在页面，如果有关闭弹出层
		$("div.div_dim:visible").each(function(){
			$(this).hide();
			$(this).children("div.div_dim2").mCustomScrollbar("destroy");
			$(this).children("div.right_arrow").css({"display":"none"});
		});
	});
}

/**
 * 将报表的运行结果信息存放到div中,一个规则对应一个div，默认都隐藏
 * @param data
 */
function createDiv(data)
{
	if (data != null && data != "")
	{
		for (var i=0; i<data.length; i++)
		{
			var reportResults = data[i].reportResultList;
			if (reportResults!=null && reportResults!="")
			{
				var objDiv = document.createElement("div");
				objDiv.id = "div_"+data[i].ruleid;
				objDiv.style.display = "none";
				
				var html = "";
				
        		html += "<div class='content'>";
        		html += "<div class='up'>";
				html += "</div>";
				html += "<div class='img_center'>";
				html += "<div style='padding-left: 8px;padding-right: 8px;'>";
				html += "<table class='table_dim'>";
				html += "<tr>";
				html += "<td width='60px'><span class='label'>名称:</span></td>";
				html += "<td>"+data[i].ruleName+"</td>";
				html += "<td><img src='"+ctx+"/images/speedreport/close.png' title='关闭' onclick='hideDiv(\""+data[i].ruleid+"\");' style='float: right;padding-right: 5px;' /></td>";
				html += "</tr>";
				html += "<tr>";
				html += "<td width='60px'><span class='label'>日期:</span></td>";
				html += "<td colspan='2'>"+reportResults[0].dataDate+"</td>";
				html += "</tr>";
				html += "</table>";
				html += "</div>";
				
				html += "<div class='div_dim2'>";
				html += "<table class='table_dim'>";
				for (var j=0; j<reportResults.length; j++)
				{
					html += "<tr>";
					html += "<td width='60px'><span class='label'>告警级别:</span></td>";
					html += "<td><span class='"+warnLevelColor(reportResults[j].warnLevel)+"' >"+reportResults[j].warnLevelName+"</span></td>";
					html += "</tr>";
					
					html += splitInfo("维度", reportResults[j].dimInfo, '');
					
					var className = "";
					//是否div的最后一行
					if (j == reportResults.length-1)
					{
						className = "last";
					}
					else{
						className = "real-line";
					}
					html += splitInfo("运行结果", reportResults[j].ruleContent, className);
					
				}
				html += "</table>";
				html += "</div>";
				html += "</div>";
				html += "<div class='down'>";
				html += "</div>";
				html += "</div>";
//				html += "<div style='clear:both'></div>";
				html += "<div class='right_arrow'></div>";
       		 	
				objDiv.innerHTML = html;
				document.getElementById("showGZDiv").appendChild(objDiv);
			}
		}
	}
}

/**
 * 如果显示内容长度>25则拆分成多行，否则只显示一行
 * @param title 标题
 * @param content 显示内容
 * @param className 最后一行的class名称
 */
function splitInfo(title, content, className)
{
	if (typeof content == undefined 
			|| content == null)
	{
		content = "";
	}
	var rowCount = 0;
	if (content.length == 0)
	{
		rowCount = 1;
	}
	else
	{
		if (content.length  % 25 == 0)
		{
			rowCount = content.length/25;
		}else
		{
			rowCount = parseInt(content.length/25) + 1;
		}
	}
	
	var html = "";
	//内容的字数不超过1行则只显示一个tr
	if (rowCount == 1)
	{
		html += "<tr class='"+className+"'>";
		html += "<td width='60px'><span class='label'>"+title+":</span></td>";
		html += "<td>"+content+"</td>";
		html += "</tr>";
	}
	
	//内容字数超过一行
	else
	{
		for (var i=0;i<rowCount;i++)
		{
			if (i == 0)
			{
				html += "<tr>";
				html += "<td width='60px'><span class='label'>"+title+":</span></td>";
				html += "<td >"+content.substr(0, 25)+"</td>";
				html += "</tr>";
			}
			//最后一行
			else if (i == rowCount-1)
			{
				html += "<tr class='"+className+"'>";
				html += "<td width='60px'></td>";
				html += "<td >"+content.substr(i*25, 25)+"</td>";
				html += "</tr>";
			}
			else
			{
				html += "<tr>";
				html += "<td width='60px'></td>";
				html += "<td>"+content.substr(i*25, 25)+"</td>";
				html += "</tr>";
			}
		}
	}
	
	
	return html;
}

/**
 * 根据告警级别，告警内容显示相应的颜色
 * @param warnLevel
 */
function warnLevelColor(warnLevel)
{
	var className = "";
	
	//严重告警
	if (warnLevel == 3)
	{
		className = "warn_serious";
	}
	
	//重要告警
	else if (warnLevel == 2)
	{
		className = "warn_important";
	}
	
	//一般告警
	else if (warnLevel == 1)
	{
		className = "warn_common";
	}
	
	//正常
	else
	{
		className = "warn_normal";
	}
	
	return className;
}

/**
 * 根据周期类型获取时间点的单位信息
 * 月：单位是日
 * 日：单位是点
 */
function getCheckTimeUnit()
{
	var periodType = $("#periodType").val();
	var unitName = "";
	//周期类型为月
	if(periodType == '2'){
		unitName = "日";
	}
	
	else if(periodType == '3'){
		unitName = "点";
	}
	
	return unitName;
}
